﻿using System;

namespace CSharpConsoleTestSolution.Demo {
    /**
     * P29: 接口, 依赖反转(dependency inversion), 单元测试
     */
    public class P29_interface_di_unit_test {

        /**
         * 接口与单元测试, 时间: 43:00
         * 接口与单元测试
         *  ·接口的产生: 自底向上(重构)，自顶向下(设计)
         *  ·C#中接口的实现 (隐式，显式，多接口)
         *  ·语言对面向对象设计的内建支持: 依赖反转，接口隔离，开/闭原则......
         *
         * TODO: 单元测试用的vs, Rider不知道怎么测试
         */
        public static void unit_test() {
            Console.WriteLine("1.接口与单元测试: 可以创建不同电压的电源, 测试电风扇的工作结果");
            Console.WriteLine("IPowerSupply power = new PowerSupply();");
            Console.WriteLine("DeskFan fan = new DeskFan(power);");
            Console.WriteLine("var expected = \"Work fine\";");
            Console.WriteLine("var actual = fan.Work();");
            Console.WriteLine("Assert.Equal(expected, actual);");
            
            IPowerSupply power = new PowerSupply();
            DeskFan fan = new DeskFan(power);
            var expected = "Work fine";
            var actual = fan.Work();
            // Assert.Equal(expected, actual);
            Console.WriteLine(actual);
        }
        
        /**
         * 单元测试 Moq 框架, 时间: 1:02:20
         */
        public static void unit_Moq() {
            Console.WriteLine();
            Console.WriteLine("2.单元测试 Moq 框架");
            Console.WriteLine("var mock = new Mock<IPowerSupply>();");
            Console.WriteLine("mock.Setup(ps => ps.GetPower()).Returns(() => 100);");
            Console.WriteLine("DeskFan fan = new DeskFan(mock.Object);");
            Console.WriteLine("var expected = \"Work fine\";");
            Console.WriteLine("var actual = fan.Work();");
            Console.WriteLine("Assert.Equal(expected, actual);");
        }



        private class DeskFan {
            private IPowerSupply _powerSupply;
            public DeskFan(IPowerSupply powerSupply) {
                _powerSupply = powerSupply;
            }
            public string Work() {
                var power = _powerSupply.GetPower();
                if (power <= 0) return "Won't work.";
                if (power < 100) return "Slow";
                if (power < 200) return "Work fine";
                return "Warning!";
            }
        }
        interface IPowerSupply {
            int GetPower();
        }
        private class PowerSupply: IPowerSupply {
            public int GetPower() {
                return 100;
            }
        }
    }
}